package cn.xgj.sfzy.service.impl;

import cn.xgj.sfzy.entity.Question;
import cn.xgj.sfzy.entity.User;
import cn.xgj.sfzy.mapper.QuestionMapper;
import cn.xgj.sfzy.mapper.UserMapper;
import cn.xgj.sfzy.service.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName QuestionServiceImpl
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月24日 上午10:41
 * @Version 1.0
 */
@Service
public class QuestionServiceImpl implements QuestionService{
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public int add(Question question) {
        return questionMapper.insert(question);
    }

    @Override
    public int edit(Question question) {
        return questionMapper.update(question);
    }

    @Override
    public int remove(Integer id) {
        return questionMapper.delete(id);
    }

    @Override
    public List<Question> findAll(Question question) {
        List<Question> list = questionMapper.find(question);
        for(Question q : list){
            Long userId = q.getUserId();
            User user = userMapper.findById(userId.intValue());
            q.setUser(user);
        }
        return list;
    }

    @Override
    public Question findById(Integer id) {
        Question q = questionMapper.findById(id);
        Long userId = q.getUserId();
        User user = userMapper.findById(userId.intValue());
        q.setUser(user);
        return q;
    }

    @Override
    public PageInfo<Question> findByPage(Integer pageNum, Integer ageSize, Question question) {
        PageHelper.startPage(pageNum, ageSize);
        List<Question> list = questionMapper.find(question);
        for(Question q : list){
            Long userId = q.getUserId();
            User user = userMapper.findById(userId.intValue());
            q.setUser(user);
        }

        PageInfo<Question> pageInfo = new PageInfo<Question>(list);
        return pageInfo;
    }
}
